<?php

class likeBtnsWidget extends CInputWidget
{
	/**
	 * Assets package ID.
	 */
	const PACKAGE_ID = 'likeBtns';
	/**
	 * Init widget.
	 */
	public function init()
	{
		parent::init();
		$cs = $this->registerClientScript();
		echo '<div class="likeBtns">';
		$this->renderVk($cs);
		$this->renderTw($cs);
		$this->renderFb($cs);
		echo '</div>';
	}
	/**
	 * Register CSS and Script.
	 */
	protected function registerClientScript()
	{
		/** @var $cs \CClientScript */
		$cs = Yii::app()->clientScript;
		if (!isset($cs->packages[self::PACKAGE_ID])) {
			/** @var $am \CAssetManager */
			$am = Yii::app()->assetManager;
			$cs->packages[self::PACKAGE_ID] = array(
					'basePath' => dirname(__FILE__),
					'baseUrl' => $am->publish(dirname(__FILE__)),
					'css' => array(
							'style.css',
					),
			);
		}
		$cs->registerPackage(self::PACKAGE_ID);
		return $cs;
	}
	protected function renderVk($cs)
	{
		$cs->registerScriptFile('//vk.com/js/api/openapi.js?86');
		echo '<div class="vk btn">';
		echo '<!-- Put this script tag to the <head> of your page -->
			<script type="text/javascript">
			  VK.init({apiId: 3528460, onlyWidgets: true});
			</script>
			<div id="vk_like"></div>
			<script type="text/javascript">
				VK.Widgets.Like("vk_like", {type: "mini"});
			</script>';
		echo '</div>';
	}
	protected function renderTw($cs)
	{
		echo '<div class="tw btn">';
		echo '<a href="https://twitter.com/share" class="twitter-share-button" data-lang="ru">Твитнуть</a>
			<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>';
		echo '</div>';
	}
	protected function renderFb($cs)
	{
		echo '<div class="fb btn">';
		echo '<div class="fb-like" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div>';
		echo '</div>';
		$cs->registerScript(
				__CLASS__ . '#' . $this->id,
				'(function(d, s, id) {
				  var js, fjs = d.getElementsByTagName(s)[0];
				  if (d.getElementById(id)) return;
				  js = d.createElement(s); js.id = id;
				  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=430635720362779";
				  fjs.parentNode.insertBefore(js, fjs);
				}(document, "script", "facebook-jssdk"));',
				\CClientScript::POS_READY
		);
	}
}
	